package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.admin.bo.AdminProfilePasswordBo;
import com.cskaoyan.market.service.AdminProfileService;
import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.db.mapper.MarketAdminMapper;
import com.cskaoyan.market.util.Md5Utils;
import com.cskaoyan.market.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;

/**
 * @author 小焦
 * @since 2023/03/22 21:46
 */
public class AdminProfileServiceImpl implements AdminProfileService {
	@Override
	public int password(AdminProfilePasswordBo passwordBo, MarketAdmin adminInfo) {
		SqlSession sqlSession = MyBatisUtil.getSession();
		MarketAdminMapper adminMapper = sqlSession.getMapper(MarketAdminMapper.class);

		// 通过md5 来加密 管理员密码 , 避免数据库密码的明文
		String password = Md5Utils.getMd5(passwordBo.getOldPassword(), adminInfo.getUsername());
		MarketAdmin marketAdmin1 = adminMapper.selectByPrimaryKey(adminInfo.getId());
		if(!password.equals(marketAdmin1.getPassword())){
			return 405;
		}
		String newPassword = Md5Utils.getMd5(passwordBo.getNewPassword(), adminInfo.getUsername());
		// passwordBo.setNewPassword(newPassword);
		// passwordBo.setNewPassword2(newPassword);
		// MarketAdmin marketAdmin = ReflectUtils.copyObject(passwordBo, MarketAdmin.class);
		MarketAdmin marketAdmin = new MarketAdmin();
		marketAdmin.setPassword(newPassword);
		marketAdmin.setId(adminInfo.getId());
		LocalDateTime now = LocalDateTime.now();
		// marketAdmin.setLastLoginIp(req.getRemoteAddr());
		// marketAdmin.setLastLoginTime(now);
		marketAdmin.setUpdateTime(now);
		System.out.println(marketAdmin);
		int row = adminMapper.updateByPrimaryKeySelective(marketAdmin);

		// 关闭SqlSession
		sqlSession.commit();
		sqlSession.close();

		return 0;

	}
}
